
#include <gtk/gtk.h>

#include "public.h"
#include "protocol.h"
#include "handle.h"

#include "win_main.h"

static GtkWidget *window;
static GtkWidget *widget;
static GtkBuilder *builder;

int msg_group_list_reply(const struct u_session_t *session, const char *data, const int len);
int msg_group_create_reply(const struct u_session_t *session, const char *data, const int len);
int msg_user_join_reply(const struct u_session_t *session, const char *data, const int len);

void win_main_init(void)
{
	builder = gtk_builder_new();

	gtk_builder_add_from_file(builder, "main_window.glade", NULL);
	gtk_builder_connect_signals(builder, NULL);

	window = gtk_builder_get_object(builder, "main_window");
	g_signal_connect(window, "destroy", (void *)gtk_main_quit, NULL);

	widget = gtk_builder_get_object(builder, "button_close");
	g_signal_connect(widget, "clicked", (void *)gtk_main_quit, NULL);

	msg_add(MSG_GROUP_LIST_REPLY, msg_group_list_reply);
	msg_add(MSG_GROUP_CREATE_REPLY, msg_group_create_reply);
	msg_add(MSG_USER_JOIN_REPLY, msg_user_join_reply);

	widget = gtk_builder_get_object(builder, "treeview_list");
	
	GtkCellRenderer *renderer_id, *renderer_name;
	renderer_id = gtk_cell_renderer_text_new();
	renderer_name = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(widget, -1, 
		"Id", renderer_id, "text", 0, NULL);
	gtk_tree_view_insert_column_with_attributes(widget, -1,
		"Name", renderer_name, "text", 1, NULL);	

	GtkListStore *liststore = gtk_builder_get_object(builder, "liststore_channel");
	gtk_tree_view_set_model(widget, liststore);

	GtkTreeIter iter;
	gtk_list_store_clear(liststore);
	gtk_list_store_append(liststore, &iter);
	gtk_list_store_set(liststore, &iter,  0,  "hello", 1, "world", -1);
	gtk_list_store_set(liststore, &iter,  0,  "hello", 1, "world", -1);
	
	return;
}

void win_main_show(void)
{
	gtk_widget_show_all(window);

	return;
}

void win_main_hide(void)
{
	gtk_widget_hide(window);

	return;
}

void on_btn_flush_clicked()
{
	struct group_list_ask_t list_ask;
	memset(&list_ask,'\0', sizeof(list_ask));

	strcpy(list_ask.title,"hello world");

	pack_send(&user_session, MSG_GROUP_LIST_ASK, &list_ask, sizeof(list_ask));
}

void on_btn_create_clicked()
{
	struct group_create_ask_t create_ask;
	memset(&create_ask, '\0', sizeof(create_ask));

	widget = gtk_builder_get_object(builder, "entry_group");
	strcpy(create_ask.group_name, gtk_entry_get_text(widget));

	pack_send(&user_session, MSG_GROUP_CREATE_ASK, &create_ask, sizeof(create_ask));
}

void on_btn_join_clicked()
{
	struct user_join_ask_t join_ask;
	memset(&join_ask, '\0', sizeof(join_ask));

	// 从列表中得到
	// join_ask.group_id = xxx
	widget = gtk_builder_get_object(builder, "entry_name");
	strcpy(join_ask.user_name, gtk_entry_get_text(widget));

	pack_send(&user_session, MSG_USER_JOIN_ASK, &join_ask, sizeof(join_ask));
}

int msg_group_list_reply(const struct u_session_t *session, const char *data, const int len)
{
	return 0;
}

int msg_group_create_reply(const struct u_session_t *session, const char *data, const int len)
{
	return 0;
}

int msg_user_join_reply(const struct u_session_t *session, const char *data, const int len)
{
	return 0;
}

